package com.estate.struts.actions;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;

import org.apache.commons.io.FileUtils;

import com.estate.business.CompanyStuffService;
import com.estate.utils.AnnouncementUtil;
import com.estate.utils.CompanyStuffUtil;
import com.estate.utils.FileUtil;
import com.estate.vo.Vo;
import com.estate.vo.impl.CompanyStuffVo;
import com.estate.vo.impl.UserInfoVo;

//少些单独上传文件方法
public class CompanyStuffAction extends EstateSupport {

	private String id;
	private String title;
	private String content;
	private String filePath;
	private int readNum;
//	private UserInfoVo uploadUser;
	private String uploadUserId;
	private Date uploadDate;
	private UserInfoVo postUser;
	private Date postDate;
	private String state;
	private String fileSaveName;
	private String fileRealName;
	
	private File file;
	private String fileName;
	
	private String method;
	
	private String multiType;
	
	private String multiDelete;
	private String delStr;
	
	private CompanyStuffService companyStuffService;
	
	@Override
	public String execute() throws Exception {
		System.out.println("method ================= " + this.getMethod());

		String page = "";
		if("add".equals(this.getMethod())){
//			System.out.println("file ======= " + this.getFile());
			CompanyStuffVo cs = this.getAddCompanyStuff();
			if(cs != null){
				boolean result = this.getCompanyStuffService().addCompanyStuff(cs,this.getFile());
				if(result){
					page=this.SUCCESS;
				}else{
					page="fail";
				}
			}else{
				page="fail";
			}
		}else if("list".equals(this.getMethod())){
			List<Vo> csList = this.getCompanyStuffService().getAllCompanyStuff(this.getSessionUser());
			this.getRequest().setAttribute("csList", csList);
			if(this.getMultiType() != null && this.getMultiType().equals("true")){
				this.getRequest().setAttribute("multiType", new Boolean(true));
			}else{
				this.getRequest().setAttribute("multiType", new Boolean(false));
			}
			page="allCSInfo";
		}else if("detail".equals(this.getMethod())){
			Vo csInfo = this.getCompanyStuffService().getCompanyStuff(Integer.parseInt(this.getId()));
			this.getRequest().setAttribute("csInfo", csInfo);
			page = "detail";
		}else if("modify".equals(this.getMethod())){
			Vo csInfo = this.getCompanyStuffService().getCompanyStuff(Integer.parseInt(this.getId()));
			this.getRequest().setAttribute("csInfo", csInfo);
			page = "update";
		}else if("update".equals(this.getMethod())){
			CompanyStuffVo cs = this.getUpdateCompanyStuff();
			if(cs == null){
				page = "fail";
			}else{
				boolean result  = this.getCompanyStuffService().updateCompanyStuff(cs,this.getFile());
				if(result){
					page=this.SUCCESS;
				}else{
					page="fail";
				}
			}
		}else if("post".equals(this.getMethod())){
			this.getCompanyStuffService().updatePost(Integer.parseInt(this.getId()));
			page = this.SUCCESS;
		}else if("delete".equals(this.getMethod())){
			if("true".equals(this.multiDelete)){
				System.out.println(this.getDelStr().split(",").length);
				boolean result = this.getCompanyStuffService().deleteMultiCS(this.getDelStr());
				if(result){
					page=this.SUCCESS;
				}else{
					page="fail";
				}
				
			}else{
				if(this.getId() == null || "".equals(this.getId())){
					page="faild";
				}else{
					CompanyStuffVo cs  = new CompanyStuffVo();
					cs.setId(Integer.parseInt(this.getId()));
					boolean result = this.getCompanyStuffService().deleteCompanyStuff(cs);
					if(result){
						page=this.SUCCESS;
					}else{
						page="fail";
					}
				}
			}
		}else{
			page="fail";
		}
		return page;
	}
	private void setCSFile(CompanyStuffVo cs){
		if(this.getFileName() != null && !"".equals(this.getFileName())){
			if(this.getFile() != null){
				String saveName = FileUtil.getReplaceFileName(this.getFileName());
				String savePath = FileUtil.COMPANYSTUFF_FILE+saveName;
				
				cs.setFilePath(savePath);
				try {
					cs.setFileRealName(new String(this.getFileName().getBytes("ISO-8859-1"),"UTF-8"));
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
				cs.setFileSaveName(saveName);
				cs.setUploadDate(new Date());
				cs.setUploadUser(this.getSessionUser());
			}
		}
	}
	private CompanyStuffVo getUpdateCompanyStuff(){
		CompanyStuffVo cs = new CompanyStuffVo();
		cs.setId(Integer.parseInt(this.getId()));
		cs.setContent(this.getContent());
		this.setCSFile(cs);
		cs.setTitle(this.getTitle());
		int stateInt = Integer.parseInt(this.getState());
		cs.setState(CompanyStuffUtil.getCompanyStuffString(stateInt));
		return cs;
	}
	private CompanyStuffVo getAddCompanyStuff(){
		CompanyStuffVo cs = new CompanyStuffVo();
		
			cs.setContent(this.getContent());
			this.setCSFile(cs);
			cs.setPostUser(this.getSessionUser());
			int stateInt = Integer.parseInt(this.getState());
			cs.setState(CompanyStuffUtil.getCompanyStuffString(stateInt));
			if(stateInt == 2){
				cs.setPostDate(new Date());
				cs.setReadNum(0);
			}
			cs.setTitle(this.getTitle());
		
		return cs;
	}

	

	public String getMethod() {
		return method;
	}

	public void setMethod(String method) {
		this.method = method;
	}

	public CompanyStuffService getCompanyStuffService() {
		return companyStuffService;
	}
	public void setCompanyStuffService(CompanyStuffService companyStuffService) {
		this.companyStuffService = companyStuffService;
	}
	public String getMultiType() {
		return multiType;
	}

	public void setMultiType(String multiType) {
		this.multiType = multiType;
	}
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public String getFilePath() {
		return filePath;
	}
	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}
	public int getReadNum() {
		return readNum;
	}
	public void setReadNum(int readNum) {
		this.readNum = readNum;
	}
	public String getUploadUserId() {
		return uploadUserId;
	}
	public void setUploadUserId(String uploadUserId) {
		this.uploadUserId = uploadUserId;
	}
	public Date getUploadDate() {
		return uploadDate;
	}
	public void setUploadDate(Date uploadDate) {
		this.uploadDate = uploadDate;
	}
	public UserInfoVo getPostUser() {
		return postUser;
	}
	public void setPostUser(UserInfoVo postUser) {
		this.postUser = postUser;
	}
	public Date getPostDate() {
		return postDate;
	}
	public void setPostDate(Date postDate) {
		this.postDate = postDate;
	}
	public String getState() {
		return state;
	}
	public void setState(String state) {
		this.state = state;
	}
	public String getMultiDelete() {
		return multiDelete;
	}
	public void setMultiDelete(String multiDelete) {
		this.multiDelete = multiDelete;
	}
	public String getDelStr() {
		return delStr;
	}
	public void setDelStr(String delStr) {
		this.delStr = delStr;
	}
	public File getFile() {
		return file;
	}
	public void setFile(File file) {
		this.file = file;
	}
	public String getFileName() {
		return fileName;
	}
	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	public String getFileSaveName() {
		return fileSaveName;
	}
	public void setFileSaveName(String fileSaveName) {
		this.fileSaveName = fileSaveName;
	}
	public String getFileRealName() {
		return fileRealName;
	}
	public void setFileRealName(String fileRealName) {
		this.fileRealName = fileRealName;
	}
}
